package tcinert;
/*
 * Diplomova Prace: Vyuziti embedded modulu pro monitoring zivotnich funkci
 *                  pacienta 
 * Trida: WatchDog.java 
 * Autor: Tomas Cinert 
 * Vytvoreno: 10.4.2012
 */

import com.siemens.icm.io.CommConnectionControlLines;
import com.siemens.icm.misc.Watchdog;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;

/**
 * Watchdog hlidani programu, reseni pri problemech s programem
 *
 * @Author: Tomas Cinert
 * @version 0.9.1
 *
 */
public class WatchDog implements Runnable {

    private boolean loop = true;
    private Thread vlakno = new Thread(this);
    Stav reset;
    Zaznam zaznam;
    int i = 0;

    /**
     * Vytvori instanci watchdogu
     *
     * @param reset
     * @param zaznam
     */
    public WatchDog(Stav reset, Zaznam zaznam) {
        this.reset = reset;
        this.zaznam = zaznam;

    }

    /**
     * Metoda volana pro start vlakna
     */
    public void start() {
        Watchdog.start(60);
        loop = true;
        zaznam.zapis("WatchDog startuje", typZapisu.Informativni);
        vlakno.start();
    }

    /**
     * Metoda pro ukonceni behu vlakna
     */
    public void stop() {
        loop = false;
        try {
            vlakno.join();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Metoda volana pri startu vlakna
     */
    public void run() {
        while (loop) {


            i++;        // Logovat kdy to watchdog vyrazil


            if (reset.getStav()) {
                zaznam.zapis("Watchdog ukoncuje aplikaci\nByl to" + i + ".prubeh", typZapisu.Chybovy);
                Watchdog.kick();
            }


            try {
                Thread.sleep(1000000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }

        }
    }
}